package com.chx.service.Impl;

import com.chx.dto.CustomerDto;
import com.chx.dto.CustomerPageQueryDto;
import com.chx.entity.Customers;
import com.chx.mapper.CustomerMapper;
import com.chx.result.PageResult;
import com.chx.service.CustomerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void save(CustomerDto customerDto) {
        Customers  customers = new Customers();
        BeanUtils.copyProperties(customerDto, customers);
        customerMapper.insert(customers);
    }

    @Override
    public void update(CustomerDto customerDto) {
        Customers customers = new Customers();
        BeanUtils.copyProperties(customerDto, customers);
        customerMapper.update(customers);
    }

    @Override
    public Customers getById(Long id) {
        return customerMapper.getById(id);
    }

    @Override
    public PageResult page(CustomerPageQueryDto customerPageQueryDto) {
        PageHelper.startPage(customerPageQueryDto.getPage(), customerPageQueryDto.getPageSize());
        Page<Customers> page = customerMapper.pageQuery(customerPageQueryDto);
        long total = page.getTotal();
        List<Customers> records = page.getResult();
        return new PageResult(total,  records);
    }

    @Override
    public List<Customers> getAllCustomers() {
        return customerMapper.getAllCustomers();
    }
}
